package com.jdaw.datastructure.stack;

public class Calculator {
    public static void main(String[] args) {
        String expression="3+2*6-2";
        Stack numStack=new Stack(10);
        Stack operStack=new Stack(10);
        int index=0;
        int num1=0;
        int num2=0;
        int res=0;
        int oper=0;
        char ch=' ';
        String keepNum="";
        while(true){
            ch=expression.substring(index,index+1).charAt(0);
            if(operStack.isOper(ch)){
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
                        oper=operStack.pop();
                        num1=numStack.pop();
                        num2=numStack.pop();
                        res=numStack.cal(num1,num2,oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }else{
                        operStack.push(ch);
                    }
                }else{
                    operStack.push(ch);
                }
            }else{
                keepNum+=ch;

                //ch可能是expression的最后一位
                if(index==expression.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else{
                    if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        //可能后一位也是数字，是多位数
                        numStack.push(Integer.parseInt(keepNum));
                        //keepNum需要清空
                        keepNum = "";
                    }
                }

            }
            index++;
            if(index>=expression.length()){
                break;
            }
        }

        //扫描完毕
        while(true){
            if(operStack.isEmpty()){
                break;
            }
            oper=operStack.pop();
            num1=numStack.pop();
            num2=numStack.pop();
            res=numStack.cal(num1,num2,oper);
            numStack.push(res);
        }
        System.out.println(numStack.peek());
    }
}


class Stack{
    private int maxSize;
    private int[] stack;
    private int top=-1;

    public Stack(int maxSize){
        this.maxSize=maxSize;
        stack=new int[this.maxSize];
    }
    public boolean isFull(){
        return top==maxSize-1;
    }
    public boolean isEmpty(){
        return top==-1;
    }

    //返回栈顶的值，但不出栈
    public int peek(){
        return stack[top];
    }
    public void push(int value){
        if(isFull()){
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top]=value;
    }
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空，无法抛出");
        }
        int value=stack[top];
        top--;
        return value;
    }
    public void list(){
        if(isEmpty()){
            throw new RuntimeException("栈空，没有数据");
        }
        for(int i=top;i>=0;i--){
            System.out.println(stack[i]);
        }
    }
    //返回运算符优先级
    public int priority(int oper){
        if(oper=='*'||oper=='/'){
            return 1;
        }else if(oper=='+'||oper=='-'){
            return 0;
        }else{
            return -1;
        }
    }
    //判断是否为运算符
    public boolean isOper(char val){
        return val=='*'||val=='/'||val=='+'||val=='-';
    }
    //计算
    public int cal(int num1,int num2,int oper){
        int res=0;
        switch (oper) {
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res=num1/num2;
                break;
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;// 注意顺序
                break;
            default:
                break;
        }
        return res;
    }
}
